package com.book.filter;

import com.book.entity.User;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/**
 * 拦截所有 非 登录的界面和 非 静态资源
 * 也就是 登录界面和静态资源 放行
 */
@WebFilter("/*")
public class MainFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        String url = req.getRequestURL().toString();
        if(!url.contains("/static/") && !url.endsWith("login")){
            HttpSession session = req.getSession();
            User user = (User) session.getAttribute("user");
            if(user == null){
                res.sendRedirect("login");//重定向到登录界面
                return;
            }
        }
        chain.doFilter(req,res);
    }
}
